package com.kldy8.kldyw;

import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.kldy8.kldyw.filmfactory.FilmTalent.FilmTalentActivity;
import com.kldy8.kldyw.logre.LoginActivity;
import com.kldy8.kldyw.system.UrlBuilder;
import com.kldy8.kldyw.web.AppWebUrl;
import com.kldy8.kldyw.filmfactory.company.CompanyActivity;
import com.kldy8.kldyw.filmfactory.dteam.DteamListActivity;
import com.kldy8.kldyw.filmfactory.equipment.DeviceActivity;
import com.kldy8.kldyw.filmfactory.shootingvenue.ShootingVenueActivity;
import com.mcxiaoke.bus.Bus;
import com.mcxiaoke.bus.annotation.BusReceiver;

/**
 * Created by Administrator on 2016/7/18.
 * 首页
 */
public class WebCircleFriendsFragment extends Fragment
{
    private WebView webView;
    private String urlString;
    private String news_id;
    private Main2Activity activity;

    private RelativeLayout reloadlayout;
    private RelativeLayout loadinglayout;
    private ImageView reload_img;
    private ProgressBar pb;

    public static WebCircleFriendsFragment newInstance(String param1)
    {
        WebCircleFriendsFragment fragment = new WebCircleFriendsFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    public WebCircleFriendsFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_web_circle_friends, container, false);
        activity = (Main2Activity) getActivity();
        pb= (ProgressBar) view.findViewById(R.id.pb);
        urlString = AppWebUrl.createUrl(AppWebUrl.MAIN);
        init(view,urlString);
        Bus.getDefault().register(this);
        return view;
    }
    /**
     * 回调  处理刷新
     *
     * @param event
     */
    @BusReceiver
    public void onStringEvent(String event) {
        if (event.equals("webload")) {
//            webView.loadUrl(urlString);
            reLoading();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Bus.getDefault().unregister(this);
    }

    private void init(View view, final String urlString)
    {
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        toolbar.setTitle(getResources().getString(R.string.my_null));//设置主标题
        toolbar.inflateMenu(R.menu.menu_home);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
        {
            @Override
            public boolean onMenuItemClick(MenuItem item)
            {
                Intent intent = new Intent();
                String resultUrl = "";
                switch (item.getItemId()) {
                    case R.id.menu_search:
                        resultUrl = AppWebUrl.createUrl(AppWebUrl.URL_SEARCH_INDEX);
                        intent.putExtra("url",resultUrl);
                        intent.setClass(activity, MyWebActivity.class);
                        activity.startActivityRightToLeft(intent);
                        break;
                }
                return true;
            }
        });


        loadinglayout = (RelativeLayout) view.findViewById(R.id.loadinglayout);
        reloadlayout = (RelativeLayout) view.findViewById(R.id.reloadlayout);
        reload_img = (ImageView) view.findViewById(R.id.reload_img);
        reload_img.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                loadinglayout.setVisibility(View.VISIBLE);
                webView.loadUrl(urlString);
            }
        });

        webView = (WebView) view.findViewById(R.id.webView);
        WebSettings settings = webView.getSettings();
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        settings.setJavaScriptEnabled(true);
        //WebView加载web资源
        Log.e("WebCircleFriendsFragment",urlString);
        webView.loadUrl(urlString);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    pb.setVisibility(View.GONE);
                } else {
                    pb.setVisibility(View.VISIBLE);
                    pb.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }

        });

        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl)
            {
                // TODO Auto-generated method stub
//                super.onReceivedError(view, errorCode, description, failingUrl);
                loadinglayout.setVisibility(View.GONE);
                reloadlayout.setVisibility(View.VISIBLE);
            }


            @Override
            public void onPageFinished(WebView view, String url)
            {
                super.onPageFinished(view, url);
                loadinglayout.setVisibility(View.GONE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                Log.e("this is my CircleFriends Url",url);
                reloadlayout.setVisibility(View.GONE);
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器

                if (url.contains("10086"))
                {
                    onReceivedError( view, 500,
                            "fuck", url);
                    return true;
                }
                if (!url.contains("kldy")&&(!url.contains("http://192.168.1.200")))
                {
                    onReceivedError( view, 500,
                            "fuck", url);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/index.php/user/login")||url.contains(UrlBuilder.URL_BASE_API+"/user/login"))
                {
                    Intent intent = new Intent();
                    intent.setClass(activity, LoginActivity.class);
                    activity.startActivityRightToLeft(intent);
                    return true;
                }

                if (url.contains(" tel:"))
                {
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                    startActivity(intent);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/index.php/user/login")||url.contains(UrlBuilder.URL_BASE_API+"/user/login"))
                {
                    Intent intent = new Intent();
                    intent.setClass(activity, LoginActivity.class);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
                 if (url.contains("10086"))
                {
                    onReceivedError( view, 500,
                    "fuck", url);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/job/person_list"))
                {
                    Intent intent = new Intent();
                    intent.setClass(activity, FilmTalentActivity.class);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/dteam/dteam_list"))
                {
                    Intent intent = new Intent();
                    intent.setClass(activity, DteamListActivity.class);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/place/place_list"))
                {
                    Intent intent = new Intent();
                    intent.setClass(activity, ShootingVenueActivity.class);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/device/device_list"))
                {
                    Intent intent = new Intent();
                    intent.setClass(activity, DeviceActivity.class);
                    activity.startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                    return true;
                }
                if (url.contains(UrlBuilder.URL_BASE_API+"/company/company_list"))
                {
                    String type = "15";
                    if (url.contains("type_id=15"))
                    {
                        type = "15";
                    }
                    if (url.contains("type_id=14"))
                    {
                        type = "14";
                    }
                    if (url.contains("type_id=16"))
                    {
                        type = "16";
                    }
                    if (url.contains("type_id=17"))
                    {
                        type = "17";
                    }
                    Intent intent = new Intent();
                    intent.putExtra("type_id",type);
                    intent.setClass(activity, CompanyActivity.class);
                    activity.startActivityRightToLeft(intent);
                    return true;
                }
                if (url.equals(UrlBuilder.URL_BASE_API+"/"))
                {
                    getActivity().recreate();
                    return true;
                }
                else
                {
                    Intent intent = new Intent();
                    String resultUrl = AppWebUrl.createUrl(url);
                    intent.putExtra("url",resultUrl);
                    intent.setClass(activity, MyWebActivity.class);
                    activity.startActivityRightToLeft(intent);
                    return true;
                }
            }
        });
    }

    public void reLoading()
    {
        if (webView != null)
        {
            webView.loadUrl(urlString);
        }
    }

}
